package utils

import (
	"net"
	"strconv"
	"strings"
)

func GetuIntString(digit uint) string {
	return strconv.Itoa(int(digit))
}

func GetBetweenStr(str, start, end string) string {
	n := strings.Index(str, start)
	if n == -1 {
		n = 0
	} else {
		n = n + len(start) // 增加了else，不加的会把start带上
	}
	str = string([]byte(str)[n:])
	m := strings.Index(str, end)
	if m == -1 {
		m = len(str)
	}
	str = string([]byte(str)[:m])
	return str
}

func GetIpFormXaddr(xaddr string) string {

	s := strings.Index(xaddr, "http://")
	if s == -1 {
		return ""
	}
	xaddr = string([]byte(xaddr)[s+len("http://"):])

	//fmt.Println(xaddr[0:])

	e := strings.Index(xaddr, "/")
	if e == -1 {
		return ""
	}
	if net.ParseIP(xaddr[0:e]) != nil {
		return xaddr[0:e]
	}
	mid := strings.Index(xaddr, ":")
	if mid == -1 {
		return ""
	}
	if net.ParseIP(xaddr[0:mid]) != nil {
		//fmt.Println(xaddr[0:mid])

		return xaddr[0:mid]
	}
	return ""
}
